/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {

    var toolbar = '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-2">';
    toolbar += '<a class="btn btn-sm btn-success" href="/manager/fee/create-electric.html">添加</a>';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-10">';
    toolbar += '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-6">';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-6">';
    toolbar += '<select class="form-control select-status"><option value="">支付状态</option><option value="1">未支付</option><option value="2">已支付</option></select>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';

    loadList('/v1/manager/fee/2/list.json', toolbar);

    $(document).on({
        change: function () {
            refreshList();
        }
    },'.select-status');

    $('#view-fee-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var name = button.data('name');
        var rent_user = button.data('rent-user');
        var fee_time = button.data('fee-time');
        var last_process = button.data('last-process');
        var fee_process = button.data('fee-process');
        var fee_num = button.data('num');
        var fee = button.data('fee');
        var pay_type = button.data('pay-type');
        var remark = button.data('remark');
        $(this).find('.fee-id').val(id);
        $(this).find('.apart-name').text(name);
        $(this).find('.rent-user-name').text(rent_user);
        $(this).find('.fee-time').text(fee_time);
        $(this).find('.last-process').text(last_process + '度');
        $(this).find('.fee-process').text(fee_process + '度');
        $(this).find('.fee-num').text(fee_num + '度');
        $(this).find('.fee').text('¥ ' + fee);
        $(this).find('input:radio').each(function(){
            $(this).removeAttr('checked');
            if ($(this).val() == pay_type) {
                $(this).prop('checked', 'checked');
            }
        });
    });
    $('#view-fee-dialog').on('hide.bs.modal', function(){
        $(this).find('.fee-id').val('');
        $(this).find('.apart-name').text('');
        $(this).find('.rent-user-name').text('');
        $(this).find('.fee-time').text('');
        $(this).find('.last-process').text('');
        $(this).find('.fee-process').text('');
        $(this).find('.fee-num').text('');
        $(this).find('.fee').text('');
        $(this).find('input:radio').removeAttr('checked');
        $(this).find('input:radio:first').prop('checked', 'checked');
    });

    $('#view-fee-dialog .btn-save').on('click', function(){
        var that = $('#view-fee-dialog');
        var data = {
            id: that.find('.fee-id').val(),
            pay_type: that.find('input[name="pay-type"]:checked').val(),
            remark: that.find('.fee-remark').val()
        };
        if (that.find('.fee-status:checked').val() == 2) {
            data.status = 2;
        }
        if (checkForm(data)) {
            sendAjax('/v1/manager/change/pay/type', 'post', {data:data}, function(json){
                that.modal('hide');
                $('#action-tip').find('.tip-content').text(json.message);
                $('#action-tip').modal('show');
            });
        }
    });

    $('#view-pay-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var name = button.data('name');
        var rent_user = button.data('rent-user');
        var fee_time = button.data('fee-time');
        var last_process = button.data('last-process');
        var fee_process = button.data('fee-process');
        var fee_num = button.data('num');
        var fee = button.data('fee');
        var pay_at = button.data('pay-at');
        var pay_order = button.data('pay-order');
        var pay_type = pay_order == '' ? '未支付' : '已支付';
        $(this).find('.fee-id').val(id);
        $(this).find('.apart-name').text(name);
        $(this).find('.rent-user-name').text(rent_user);
        $(this).find('.fee-time').text(fee_time);
        $(this).find('.last-process').text(last_process + '度');
        $(this).find('.fee-process').text(fee_process + '度');
        $(this).find('.fee-num').text(fee_num + '度');
        $(this).find('.fee').text('¥ ' + fee);
        $(this).find('.pay-type').html(pay_type);
        $(this).find('.pay-at').text(pay_at);
        $(this).find('.pay-order').text(pay_order);
    });
    $('#view-pay-dialog').on('hide.bs.modal', function(){
        $(this).find('.fee-id').val('');
        $(this).find('.apart-name').text('');
        $(this).find('.rent-user-name').text('');
        $(this).find('.fee-time').text('');
        $(this).find('.last-process').text('');
        $(this).find('.fee-process').text('');
        $(this).find('.fee-num').text('');
        $(this).find('.fee').text('');
        $(this).find('.pay-type').html('');
        $(this).find('.pay-at').text('');
        $(this).find('.pay-order').text('');
    });

    $('#delete-fee-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        $(this).find('.fee-id').val(id);
    });
    $('#delete-fee-dialog').on('hide.bs.modal', function(){
        $(this).find('.fee-id').val('');
    });

    $('#delete-fee-dialog .btn-save').on('click', function() {
        var id = $('#delete-fee-dialog').find('.fee-id').val();
        sendAjax('/v1/manager/delete/fee', 'post', {id:id}, function(json){
            $('#delete-fee-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});
function loadList(url, toolbar) {
    $("#fee-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: toolbar,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        search: true,
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        queryParams : function (params) {
            params.status = $('.select-status').val();
            return params;
        },
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'name',
            title: '公寓名称'
        }, {
            field: 'rent_user_name',
            title: '承租人姓名（工号）'
        }, {
            field: 'fee_time',
            title: '结算范围'
        }, {
            field: 'num',
            title: '结算电量',
            formatter: function (value) {
                return value + ' 度';
            }
        }, {
            field: 'fee',
            title: '结算费用',
            formatter: function (value) {
                return '¥ ' + value;
            }
        }, {
            field: 'pay_type',
            title: '支付类型',
            formatter: function (value) {
                return getPayType(value);
            }
        }, {
            field: 'status',
            title: '支付状态',
            formatter: function (value) {
                return value == 2 ? '已支付' : '未支付';
            }
        }, {
            field: 'last_process',
            title: '上期用量',
            visible: false
        }, {
            field: 'process',
            title: '本期用量',
            visible: false
        }, {
            field: 'pay_at',
            title: '支付时间',
            visible: false
        }, {
            field: 'pay_order',
            title: '支付订单',
            visible: false
        }, {
            field: 'remark',
            title: '操作',
            formatter: function (value, row) {
                var action = '';
                if (row.status == 1) action += '<a class="btn btn-xs btn-info view-rent" data-toggle="modal" data-target="#view-fee-dialog" data-id="'+row.id+'" data-name="'+row.name+'" data-rent-user="'+row.rent_user_name+'" data-fee-time="'+row.fee_time+'" data-last-process="'+row.last_process+'" data-fee-process="'+row.process+'" data-num="'+row.num+'" data-fee="'+row.fee+'" data-pay-type="'+row.pay_type+'">详情</a>';
                if (row.status == 2) action += '<a class="btn btn-xs btn-primary view-pay" data-toggle="modal" data-target="#view-pay-dialog" data-id="'+row.id+'" data-name="'+row.name+'" data-rent-user="'+row.rent_user_name+'" data-fee-time="'+row.fee_time+'" data-last-process="'+row.last_process+'" data-fee-process="'+row.process+'" data-num="'+row.num+'" data-fee="'+row.fee+'" data-pay-type="'+row.pay_type+'" data-pay-at="'+row.pay_at+'" data-pay-order="'+row.pay_order+'">查看支付信息</a>';
                action += '<a class="btn btn-xs btn-danger delete-rent" data-toggle="modal" data-target="#delete-fee-dialog" data-id="'+row.id+'">删除</a>';
                return action;
            }
        }]
    });
}

function refreshList() {
    $('#fee-table').bootstrapTable('refresh');
}

function getPayType(pay_type) {
    switch (pay_type) {
        case 0 : return '<span class="text-default"><b>--</b></span>';
        case 1 : return '<span class="text-warning"><b>现金支付</b></span>';
        case 2 : return '<span class="text-success"><b>支付宝支付</b></span>';
        case 3 : return '<span class="text-success"><b>银行卡转账</b></span>';
        case 4 : return '<span class="text-success"><b>工资扣除</b></span>';
        default : return '<span class="text-default"><b>--</b></span>';
    }
}

function getPayStatusType(pay_type) {
    switch (pay_type) {
        case 1 : return '<span class="text-warning"><i class="fa fa-check-circle"></i>已用现金支付</span>';
        case 2 : return '<span class="text-warning"><i class="fa fa-check-circle"></i>已用支付宝支付</span>';
        case 3 : return '<span class="text-warning"><i class="fa fa-check-circle"></i>已用银行卡转账支付</span>';
        case 4 : return '<span class="text-warning"><i class="fa fa-check-circle"></i>已用工资扣除支付</span>';
    }
}

function checkForm(data) {
    if (!data.id) {
        $('#action-tip .modal-body .tip-content').text('非法请求');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.pay_type) {
        $('#action-tip .modal-body .tip-content').text('请选择支付方式');
        $('#action-tip').modal('show');
        return false;
    }
    return true;
}